//
//  LHAlert.m
//  自带AlertView封装
//
//  Created by LH on 16/8/29.
//  Copyright © 2016年 LH. All rights reserved.
//

#import "LHAlert.h"
#import "AppDelegate.h"
/*
 *物理屏幕宽度
 */
#define IphoneWidth [UIScreen mainScreen].bounds.size.width
/*
 *物理屏幕高度
 */
#define IphoneHeight [UIScreen mainScreen].bounds.size.height
//  设置颜色RGB

#define RGB_COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]

#define ViewWidth IphoneWidth*(5.00/6.00)
#define ViewHeight 120
#define butHeight 40

@interface LHAlert()
@property(nonatomic,strong)UIView *wriView;

@property(nonatomic,strong)UIButton *leftBut;
@property(nonatomic,strong)UIButton *rightBut;
@property(nonatomic,strong)UILabel *infoLabel;

@property(nonatomic,strong)UIView *butView;
@property (nonatomic,strong) UIImageView  *imageView2;

@end

@implementation LHAlert

-(UIView *)butView{
    if (!_butView) {
        _butView=[[UIView alloc]initWithFrame:CGRectMake(0, ViewHeight-butHeight, ViewWidth,butHeight)];
        
        UIImageView *imageView1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, 1)];
        imageView1.backgroundColor=[UIColor groupTableViewBackgroundColor];
        [_butView addSubview:imageView1];
        
        if (_singleBut) {

        }else{
            _imageView2=[[UIImageView alloc]initWithFrame:CGRectMake(ViewWidth/2, 0, 1,butHeight)];
            _imageView2.backgroundColor=[UIColor groupTableViewBackgroundColor];
            [_butView addSubview:_imageView2];
        }
        if (_singleBut) {
            
        }else{
            [self.butView addSubview:self.rightBut];
        }
        [self.butView addSubview:self.leftBut];
    }
    return _butView;
}

-(UIView *)wriView{
    if (!_wriView) {
        _wriView=[[UIView alloc]initWithFrame:CGRectMake((IphoneWidth-ViewWidth)/2,-200,ViewWidth,ViewHeight)];
        _wriView.layer.cornerRadius=4;
        _wriView.layer.masksToBounds=YES;
        //_wriView.layer.borderWidth=0.1;
        [_wriView addSubview:self.butView];
    }
    return _wriView;
}
/*
 *左边按钮
 */
-(UIButton *)leftBut{
    if (!_leftBut) {
        _leftBut=[UIButton buttonWithType:UIButtonTypeCustom];
        if (_singleBut) {
            _leftBut.frame=CGRectMake(0,0,ViewWidth ,butHeight);
        }else{
            _leftBut.frame=CGRectMake(0,0,ViewWidth/2-1 ,butHeight);
        }
        [_leftBut setBackgroundImage:[self imageWithColor:[UIColor groupTableViewBackgroundColor]] forState:UIControlStateHighlighted];

//        _leftBut.layer.cornerRadius=4.0f;
//        _leftBut.layer.masksToBounds=YES;
        [_leftBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_leftBut addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBut;
}
/*
 *右边按钮
 */
-(UIButton *)rightBut{
    if (!_rightBut) {
        _rightBut=[UIButton buttonWithType:UIButtonTypeCustom];
        _rightBut.frame=CGRectMake(ViewWidth/2+1, 0, ViewWidth/2, butHeight);
//        _rightBut.layer.cornerRadius=4.0f;
//        _rightBut.layer.masksToBounds=YES;
        [_rightBut setBackgroundImage:[self imageWithColor:[UIColor groupTableViewBackgroundColor]] forState:UIControlStateHighlighted];
        [_rightBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [_rightBut addTarget:self action:@selector(rightButAction) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return _rightBut;
}
/*
 *显示文字
 */
-(UILabel *)infoLabel{
    if (!_infoLabel) {
        _infoLabel=[[UILabel alloc]initWithFrame:CGRectMake(10,0,IphoneWidth*(5.00/6.00)-20,ViewHeight-butHeight)];
        _infoLabel.textAlignment=NSTextAlignmentCenter;
        _infoLabel.numberOfLines=0;
    }
    return _infoLabel;
}
- (instancetype)init{
    if (self = [super init]) {
        self.frame=CGRectMake(0, 0,IphoneWidth, IphoneHeight);
    }
    return self;
}

-(void)createView{
    
    [self addSubview:self.wriView];
    [self.wriView addSubview:self.infoLabel];
    [UIView animateKeyframesWithDuration:0.5 delay:0 options:0 animations:^{
        self.wriView.frame=CGRectMake((IphoneWidth-ViewWidth)/2,IphoneHeight/2-100,ViewWidth,ViewHeight);
        
    } completion:^(BOOL finished) {
        
    }];
    self.wriView.backgroundColor=[UIColor whiteColor];
    UIWindow * currentWindow1=  ((AppDelegate *)[UIApplication sharedApplication].delegate).window;
    self.backgroundColor=RGB_COLOR(174, 175, 176, 0.2);
    [currentWindow1.rootViewController.view addSubview:self];
}


/**
 *    设置显示文字 及左右按钮名称
 *   @author FlashMan
 */
-(void)setTitle:(NSString *)title RightButTitle:(NSString *)rightButTitle LeftButTitle:(NSString *)leftButTitle{
    
    [self createView];
    self.infoLabel.text=title;
    [self.rightBut setTitle:rightButTitle forState:UIControlStateNormal];
    [self.leftBut setTitle:leftButTitle forState:UIControlStateNormal];
}
-(void)setRightButTitleColor:(UIColor *)rightButTitleColor{
    [self.rightBut setTitleColor:rightButTitleColor forState:UIControlStateNormal];
}
-(void)setLeftButTitleColor:(UIColor *)leftButTitleColor{
    [self.leftBut setTitleColor:leftButTitleColor forState:UIControlStateNormal];
}
-(void)setTitleColor:(UIColor *)titleColor{
    [self.infoLabel setTextColor:titleColor];
}
-(void)cancelAction{
    [UIView animateKeyframesWithDuration:0.5 delay:0 options:0 animations:^{
        self.wriView.frame=CGRectMake(IphoneWidth,IphoneHeight,50,50);
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
-(void)rightButAction{
    
    if ([self.delegate respondsToSelector:@selector(rightButAction)]) {
        [self.delegate rightButAction];
    }
    [self removeFromSuperview];
    
}
//  颜色转换为背景图片
- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
